package com.example.sendmessage.server.core;

import java.io.Serializable;
import java.util.UUID;

public class Message implements Serializable {
    // 验证消息版本号
    private static final long serialVersionUID = 1;
    private BasicProperties basicProperties = new BasicProperties();
    // 消息具体内容存放处
    private byte[] body;

    // 消息持久化所需属性
    // 消息在文件开始的位置 transient表示不序列化
    private transient long offsetBeg = 0;
    // 消息在文件结束的位置
    private transient long offsetEnd = 0;
    // 消息是否存在的标记 0x1 表示存在 0x0表示不存在
    private byte isValid = 0x1;

    // 工厂方法创建消息
    public static Message createMessageWithId(String routingKey, BasicProperties basicProperties, byte[] body) {
        Message message = new Message();
        // 主要就是判断消息是否持久化
        if (basicProperties != null) {
            message.setBasicProperties(basicProperties);
        }
        // 生成消息唯一id
        message.setMessageId("M-" + UUID.randomUUID());
        message.setRoutingKey(routingKey);
        message.setBody(body);
        return message;
    }

    public BasicProperties getBasicProperties() {
        return basicProperties;
    }

    public void setBasicProperties(BasicProperties basicProperties) {
        this.basicProperties = basicProperties;
    }

    public String getMessageId() {
        return basicProperties.getMessageId();
    }

    public void setMessageId(String messageId) {
        basicProperties.setMessageId(messageId);
    }

    public String getRoutingKey() {
        return basicProperties.getRoutingKey();
    }

    public void setRoutingKey(String routingKey) {
        basicProperties.setRoutingKey(routingKey);
    }

    public int getDeliverMode() {
        return basicProperties.getDeliverMode();
    }

    public void setDeliverMode(int deliverMode) {
        basicProperties.setDeliverMode(deliverMode);
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public long getOffsetBeg() {
        return offsetBeg;
    }

    public void setOffsetBeg(long offsetBeg) {
        this.offsetBeg = offsetBeg;
    }

    public long getOffsetEnd() {
        return offsetEnd;
    }

    public void setOffsetEnd(long offsetEnd) {
        this.offsetEnd = offsetEnd;
    }

    public byte getIsValid() {
        return isValid;
    }

    public void setIsValid(byte isValid) {
        this.isValid = isValid;
    }
}
